package com.asiainfo.ftp;

import com.jcraft.jsch.*;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

public class FTPTest {
    //定义连接对象
    private static Session session = null;
    private static Channel openChannel;
    //针对于linux操作 需要创建执行操作的通道，就像文件传输一样

    public static void main(String[] args) throws JSchException, InterruptedException, SftpException {
        String hostname="192.168.100.131";
        String userName="jiang";
        String password="root";
        Integer port=22;
        boolean login = login(hostname, userName, password, port);

        getChannelOp();
//        TimeUnit.SECONDS.sleep(11);
        upFile("");

        //下载文件
        downFile();

//        session.
        System.out.println(session.isConnected());
        if (login){
            System.out.println("登录主机成功~~");
        }else {
            System.out.println("登录主机失败~~");
        }



        //使用完成一定要关闭
        session.disconnect();
    }


    /**
     *
     * @param hostname
     * @param userName
     * @param password
     * @param port
     * @return
     */
    public static boolean login(String hostname,
                         String userName,
                         String password,
                         Integer port)  {

        JSch jSch = new JSch();
        //获取Session

        try {
            session = jSch.getSession(userName,hostname , port);
            //验证密码
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setConfig("PreferredAuthentications", "password");
            session.connect(10*1000);
        } catch (JSchException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    //获取远程执行命令的对象
    public static void getChannelOp(){
        ChannelExec exec=null;
        try {
              exec = (ChannelExec) session.openChannel("exec");
//            exec.setCommand("mkdir /home/jiang/12345");
            exec.setCommand("df -h");
            exec.connect();  //连接就执行
            //执行后 接受返回值
            try {
                InputStream inputStream = exec.getInputStream();
                String result = IOUtils.toString(inputStream);
                System.out.println(result+"\n");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (JSchException e) {
            e.printStackTrace();
        }finally {
            assert exec != null;
            if (exec.isConnected()){
                exec.disconnect();
            }
        }
    }

    /**
     * 文件上传
     * @param path
     */
    public static void upFile(String path) throws InterruptedException {
//        session.disconnect();
//        TimeUnit.SECONDS.sleep(5);
        ChannelSftp channel=null;
        try {
            System.out.println("开始上传文件~~");
            channel = (ChannelSftp) session.openChannel("sftp");
            //文件上传
            channel.connect();
            channel.cd("/home/jiang");
            channel.put("C:\\Users\\admin\\Desktop\\Codes\\Spark-Code\\data\\user.text","/home/jiang/12345"
            ,ChannelSftp.OVERWRITE);

        } catch (JSchException | SftpException e) {
            e.printStackTrace();
        } finally {
            //关闭连接
            assert channel != null;
            channel.disconnect();
        }
    }



    //文件下载
    public static void downFile() throws JSchException, SftpException {
        //获取连接通道
        ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
        System.out.println("开始下载文件~~");
        channel.connect();
        //
        channel.get("/home/jiang/MapReduce-1.0-SNAPSHOT.jar","./");


        channel.disconnect();
    }
}
